import { CURRENT_LOGIN_INFO, LOGIN_OUT } from '../mutation-types'
import { save, read, clear } from '../../storage/session'
import { loginAuth,signOut } from '../../api/login.service'

const state = {
  isLogin: false,
  currentUser: {}
}

const mutations = {
  [CURRENT_LOGIN_INFO] (state, userInfo) {
    state.isLogin = true
    state.currentUser = userInfo
  },
  [LOGIN_OUT] (state) {
    state.isLogin = false
    state.currentUser = {}
  }
}
const actions = {
  saveCurrentLoginInfo ({ commit }, userInfo) {
    return new Promise(function (resolve, reject) {
        commit(CURRENT_LOGIN_INFO, userInfo)
        save(CURRENT_LOGIN_INFO, JSON.stringify(state))
        resolve()
    })
  },
  loginOut ({ commit }) {
    return new Promise(function (resolve) {
      signOut();
      commit(LOGIN_OUT)
      clear(CURRENT_LOGIN_INFO, true)
      resolve()
    })
  }
}

const getters = {
  getCurrentLoginInfo () {
    var jsd=read(CURRENT_LOGIN_INFO);
    if(jsd!=undefined&&jsd!=null&&jsd!='')
    return JSON.parse(jsd).currentUser
    return {};
  },
  getIsLogin () {
    if (JSON.parse(read(CURRENT_LOGIN_INFO)) == null) {
      return false
    }
    return JSON.parse(read(CURRENT_LOGIN_INFO)).isLogin
  }
}
export default {
  state,
  mutations,
  actions,
  getters
}
